#!/usr/bin/env python3

import optparse
import os
import sys

from venya3.lib import cmdparser
from venya3.lib import ip
from venya3.lib import stdlib
from venya3.lib import exception

import bgperr
import bgpconf

class bgpd(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-c", "--config", type = "str",
            help = "BGP daemon configuration file", default = "bgpd.cfg",
            dest = "config"),
         optparse.make_option("-d", "--debug", action = "store_true",
            help = "Enable debug", default = False, dest = "debug"),
   ) + cmdparser.GetOpt.option_list
   
   def __init__(self, **kwargs):
      super(stdlib.my_class(self), self).__init__(**kwargs)
      self._cfg = None

   def handle(self):
      (options, args) = self.parse()
      if options.config:
         parser = bgpconf.BgpConfig(options.config)
         try:
            print(parser.parse())
         except bgperr.ConfigError as err:
            raise exception.GetOptError(err)


if __name__ == "__main__":
   daemon = None
   try:
      daemon = bgpd(version = 1.0, args = sys.argv)
      daemon.handle()
   except exception.GetOptError as err:
      print(err)
      sys.exit(1)
